package cz.worsik.android.test.rideshare.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by 231260 on 4.1.14.
 */
public class DatabaseHelper extends SQLiteOpenHelper
{
    public static final String TAG = "DBAdapter";

    DatabaseHelper(Context context)
    {
        super(context, DbConstants.DATABASE_NAME, null, DbVersion.DATABASE_MAJOR_VERSION * 100 + DbVersion.DATABASE_MINOR_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(DbConstants.DATABASE_CREATE_0);
        db.execSQL(DbConstants.DATABASE_CREATE_1);
        db.execSQL(DbConstants.DATABASE_CREATE_2);
        db.execSQL(DbConstants.DATABASE_CREATE_3);
        db.execSQL(DbConstants.DATABASE_CREATE_4);
        db.execSQL(DbConstants.DATABASE_CREATE_5);
        db.execSQL(DbConstants.DATABASE_CREATE_6);

        // aktualizace verze DB
        db.execSQL("delete from version");
        ContentValues initialValues = new ContentValues();
        initialValues.put("major", DbVersion.DATABASE_MAJOR_VERSION);
        initialValues.put("minor", DbVersion.DATABASE_MINOR_VERSION);
        db.insert("version", null, initialValues);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion)
    {
        if (DbConstants.DEBUG) {
            Log.i(TAG, "Upgrading database from version " + oldVersion
                    + " to "
                    + newVersion + ", which will destroy all old data");
        }
        db.execSQL(DbConstants.DATABASE_DROP_1);
        db.execSQL(DbConstants.DATABASE_DROP_2);
        db.execSQL(DbConstants.DATABASE_DROP_3);
        db.execSQL(DbConstants.DATABASE_DROP_4);
        db.execSQL(DbConstants.DATABASE_DROP_5);
        db.execSQL(DbConstants.DATABASE_DROP_6);
        onCreate(db);
    }
}
